<template>
  <div class="mainor">
    <el-form
      :inline="true"
      :rules="rules"
      :model="monitorForm"
      class="demo-form-inline"
      ref="userListFrom"
      label-width="80px"
    >
      <div
        class="main_spac1 paddingBottom5"
        @click="(mainor_ex = false), (mainor_co = false)"
      >
        <div class="main_white padding10 paddingBottom0">
          <div class="main_crumbs boxdq">
            <div class="main_crumbs_l">
              <a href="javascript:;" @click="$router.back(-1)">
                <i class="el-icon-arrow-down"></i>返回
              </a>
              <em>|</em>
              <span>订单列表</span>
            </div>
            <div class="main_crumbs_r boxnr">
              <el-button @click.stop="mainor_ex = true">页面设置</el-button>
            </div>
          </div>
          <div class="marginBottom5 marginTop15">
            <el-form-item
              label="用户名称"
              prop="form.name_uid"
              class="marginBottom5"
              :rules="rules.name_uid"
            >
              <el-autocomplete
                v-model.trim="monitorForm.form.name_uid"
                :trigger-on-focus="true"
                ref="company"
                :disabled="true"
                :fetch-suggestions="
                  (queryString, cb) => {
                    basicquerySearchAsync(queryString, cb);
                  }
                "
                placeholder="用户名称"
                clearable
                class="wid240 main_comm"
                :highlight-first-item="true"
                value-key="nickname"
                @select="
                  (item) => {
                    basicChange(item);
                  }
                "
              >
                <template slot-scope="{ item }">
                  <div class="main_name">
                    <span> {{ item.nickname }}</span>
                    <span class="main_addr" v-if="info.show_member_code == 1">{{
                      item.info.code
                    }}</span>
                  </div>
                </template>
              </el-autocomplete>
            </el-form-item>
            <el-form-item
              label="配送日期"
              prop="form.send_time"
              :rules="rules.send_time"
            >
              <el-date-picker
                v-model="monitorForm.form.send_time"
                type="date"
                :editable="false"
                :clearable="false"
                class="time240"
                value-format="yyyy-MM-dd"
                placeholder="选择日期"
              ></el-date-picker>
            </el-form-item>
            <el-form-item
              label="配送时间"
              prop="form.send_time_id"
              :rules="rules.send_time_id"
            >
              <el-select
                v-model.trim="monitorForm.form.send_time_id"
                placeholder="配送时间"
                class="wid240"
              >
                <el-option label="无" :value="0"></el-option>
                <el-option
                  v-for="(send_time, index) in info.send_time"
                  :key="index"
                  :label="send_time"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
            </el-form-item>
            <div class="clearfix"></div>
            <el-form-item
              label="备注"
              prop="form.remark"
              class="marginRight0 marginTop10 remark_in"
            >
              <el-input
                v-model.trim="monitorForm.form.remark"
                placeholder="备注"
                clearable
              ></el-input>
            </el-form-item>
          </div>
        </div>
      </div>
      <!-- 子账户 -->
      <div
        class="main_spac1"
        style="padding-bottom: 5px; padding-top: 5px"
        @click="(mainor_ex = false), (mainor_co = false)"
      >
        <div class="main_white padding10">
          <div class="mainor_zh boxnr" style="text-align: left">
            <el-form-item
              label="子账户"
              prop="form.sub_zid"
              :rules="rules.sub_zid"
              class="marginBottom0"
              v-if="display_sub_account"
            >
              <el-select
                v-model.trim="monitorForm.form.sub_zid"
                placeholder="子账户"
                class="wid240"
                @change="sub_account"
              >
                <el-option
                  v-for="(subAccount, index) in address.subAccount"
                  :key="index"
                  :label="subAccount.nickname"
                  :value="index"
                ></el-option>
              </el-select>
            </el-form-item>
            <div style="width: 65%" class="paddingLeft10">
              <span>收货人：{{ address.address.name }}</span>
              <span>{{ address.address.mobile }}</span>
              <span style="width: 80%">{{
                address.address.address + address.address.detail
              }}</span>
            </div>
          </div>
        </div>
      </div>
      <!-- 商品名称 -->
      <div
        class="main_spac1"
        style="padding-top: 5px"
        @click="(mainor_ex = false), (mainor_co = false)"
      >
        <div class="main_white padding10">
          <div class="mainor_sp boxha">
            <ul class="box">
              <li class="box">
                <span> <em>*</em>商品名称： </span>
                <el-autocomplete
                  v-model="formsp.value"
                  ref="searchSelect"
                  class="wid240 main_comm"
                  :trigger-on-focus="true"
                  @clear="$refs.searchSelect.$children[0].blur()"
                  :fetch-suggestions="
                    (queryString, cb) => {
                      querySearchAsync(queryString, cb);
                    }
                  "
                  placeholder="请输入内容"
                  clearable
                  :highlight-first-item="true"
                  value-key="name"
                  @select="
                    (item) => {
                      handleSelect(item);
                    }
                  "
                >
                  <template slot-scope="{ item }">
                    <div class="main_name">
                      <span>
                        {{ item.goods.name }}【{{ item.spu_name.title }}】</span
                      >
                      <span class="main_addr" v-if="info.show_sku_code == 1">{{
                        item.goods_spu_code
                      }}</span>
                    </div>
                  </template>
                </el-autocomplete>
              </li>
              <li class="box">
                <span> <em>*</em>数量： </span>
                <el-input
                  v-model.trim="formsp.num"
                  ref="focusw2"
                  placeholder="数量"
                  clearable
                  class="wid140"
                  @keyup.enter.native="mainor_sp"
                ></el-input>
              </li>
            </ul>
            <div class="marginBottom10">
              <el-button type="primary" @click="mainor_qr"
                >确认(回车)</el-button
              >
              <el-button class="reset marginLeft5" type="info" @click="reset"
                >重置</el-button
              >
            </div>
            <div class="svg_box marginBottom10">
              <svg
                aria-hidden="true"
                :class="[
                  sort_order_down ? 'new_navbar_svg' : 'new_navbar_svg2',
                ]"
                width="28"
                height="28"
                @click="handleSort"
              >
                <use xlink:href="#icon-order_sort_btn" />
              </svg>
              <el-tooltip class="item" effect="dark" placement="bottom">
                <div slot="content">
                  箭头向上，新增的商品在最下面；
                  <br />
                  箭头向下，新增的商品在最上面
                </div>
                <svg
                  aria-hidden="true"
                  class="navbar_svg"
                  width="16"
                  height="16"
                >
                  <use xlink:href="#icon-summary_question" />
                </svg>
              </el-tooltip>
            </div>
          </div>
          <div class="main_table main_taf marginTop0" style="min-height: 330px">
            <el-table
              ref="multipleTable"
              :data="monitorForm.list"
              element-loading-text="加载中"
              v-loading="table_loading"
              fit
              border
              highlight-current-row
            >
              <el-table-column label="序号">
                <template slot-scope="scope">{{
                  scope.row.list_index
                }}</template>
              </el-table-column>
              <div v-for="(field, index) in info.field" :key="index">
                <el-table-column
                  label="商品图片"
                  prop="url"
                  min-width="100"
                  class-name="main_hei"
                  v-if="field == 2"
                >
                  <template slot-scope="scope">
                    <div class="demo-image__error mainor_im">
                      <el-image fit="fill" :src="scope.row.url">
                        <div slot="error" class="image-slot">
                          <img :src="puzzleImg" />
                        </div>
                      </el-image>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column
                  label="商品名称"
                  prop="name"
                  :show-overflow-tooltip="true"
                  min-width="150"
                  align="center"
                  v-if="field == 3"
                >
                  <template slot-scope="scope">
                    <div
                      style="
                        display: flex;
                        align-items: start;
                        justify-content: center;
                      "
                    >
                      <span
                        v-if="scope.row.activity_type != 0"
                        :class="'active' + scope.row.activity_type"
                        >[{{
                          activity_type_list[scope.row.activity_type]
                        }}]</span
                      >
                      {{ scope.row.name }}【{{ scope.row.spu_name }}】
                    </div>
                  </template>
                </el-table-column>
                <el-table-column
                  label="描述"
                  prop="describe"
                  v-if="field == 4"
                  :show-overflow-tooltip="true"
                />
                <el-table-column
                  label="单位"
                  prop="unit"
                  v-if="field == 5"
                  :show-overflow-tooltip="true"
                />
                <el-table-column label="现有库存" prop="num" v-if="field == 6">
                </el-table-column>
                <el-table-column
                  label="起订量"
                  prop="minimum"
                  v-if="field == 7"
                />
                <el-table-column
                  label="指定库存"
                  prop="designated_inventory"
                  v-if="field == 8"
                />
                <el-table-column
                  label="数量"
                  min-width="160"
                  class-name="main_hei"
                  v-if="field == 9"
                >
                  <template slot-scope="scope">
                    <el-form-item
                      :prop="'list.' + scope.$index + '.order_num'"
                      :rules="rules.order_num"
                      label-width="10px"
                      label=" "
                    >
                      <el-input
                        v-model.trim="scope.row.order_num"
                        :ref="'order_num' + scope.row.goods_spu_id"
                        placeholder="数量"
                        class="wid110"
                      ></el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
                <el-table-column
                  label="采购价"
                  prop="purchase_price"
                  v-if="field == 10"
                >
                  <template slot-scope="scope">
                    <span :style="{ color: scope.row.red ? 'red' : '' }">
                      {{ scope.row.purchase_price }}
                    </span>
                  </template>
                </el-table-column>
                <el-table-column
                  label="单价"
                  prop="order_price"
                  min-width="180"
                  class-name="main_hei"
                  v-if="field == 11"
                >
                  <template slot-scope="scope">
                    <el-form-item
                      :prop="'list.' + scope.$index + '.order_price'"
                      :rules="rules.order_price"
                      label-width="10px"
                      label=" "
                    >
                      <el-input
                        v-model.trim="scope.row.order_price"
                        :ref="'order_price' + scope.row.goods_spu_id"
                        placeholder="单价"
                        class="wid110"
                      ></el-input>
                      <el-popover
                        placement="bottom"
                        width="180"
                        trigger="hover"
                      >
                        <div class="order_num_tips">
                          <span>上一次单价：{{ scope.row.pre_price }}</span>
                          <span
                            >上一次采购价：{{
                              scope.row.pre_purchase_price
                            }}</span
                          >
                        </div>
                        <i
                          slot="reference"
                          style="font-size: 16px"
                          class="el-icon-question"
                        ></i>
                      </el-popover>
                    </el-form-item>
                  </template>
                </el-table-column>
                <el-table-column
                  label="小计"
                  prop="userNickname"
                  v-if="field == 12"
                >
                  <template slot-scope="scope">{{
                    (scope.row.order_num * scope.row.order_price) | twoPlace
                  }}</template>
                </el-table-column>
              </div>
              <el-table-column
                label="备注"
                prop="remark"
                width="350"
                class-name="main_hei"
              >
                <template slot-scope="scope">
                  <el-input
                    v-model.trim="scope.row.remark"
                    placeholder="备注"
                    class="wid240"
                  ></el-input>
                  <el-link
                    type="danger"
                    @click="
                      scope.row.show
                        ? monitorForm.list.length <= 1
                          ? endDelete()
                          : scdelete(scope, true)
                        : scdelete(scope)
                    "
                    >删除</el-link
                  >
                </template>
              </el-table-column>
            </el-table>
          </div>

          <div class="newConBox_p200 paddingBottom10" id="newConBox">
            <div class="newConBox_h32">
              <div
                :class="['mainor_pr boxdq newConBox ', { on: searchBarFixed }]"
              >
                <div class="mainor_prl">
                  <!-- <el-button
                    type="primary"
                    @click="preservation(1)"
                    v-repeatClick
                    >保存并继续修改</el-button
                  > -->
                  <el-button
                    type="primary"
                    @click="preservation(2)"
                    v-repeatClick
                    >保存并返回列表</el-button
                  >
                  <el-button @click="preservation(3)" v-if="order_status == 0"
                    >保存并通过审核</el-button
                  >
                  <el-button @click="$router.back(-1)">取消</el-button>
                </div>
                <div class="mainor_prr">
                  合计品种：{{ monitorForm.list.length }}件
                  <span>合计金额：¥{{ total }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </el-form>

    <!-- 页面设置 -->
    <div :class="[{ on: mainor_ex }, ' mainor_ex']">
      <div class="mainor_exb">
        <h3>子账号</h3>
        <div class="mainor_exb_an">
          <el-checkbox v-model="display_sub_account" @change="setting_company"
            >显示</el-checkbox
          >
        </div>
        <h3>表头设置</h3>
        <div class="mainor_exb_an">
          <el-checkbox-group v-model="type">
            <el-checkbox
              v-for="(Header, index) in Header"
              :key="index"
              :label="Header.id"
              :disabled="Header.disabled"
              >{{ Header.name }}</el-checkbox
            >
          </el-checkbox-group>
        </div>
      </div>
      <div class="mainor_exb_b">
        <el-button type="primary" @click="determine" v-repeatClick
          >确定</el-button
        >
        <el-button @click="recovery" v-repeatClick>恢复</el-button>
        <el-button @click="mainor_ex = false">取消</el-button>
      </div>
    </div>
  </div>
</template>

<script>
const shuju = {
  list: {
    pre_price: "",
    pre_purchase_price: "",
    activity_type: "",
    list_index: "",
    pid: "",
    category_id: "",
    pic: "",
    url: "",
    name: "",
    describe: "",
    goods_id: "",
    standard_goods_id: "",
    goods_spu_id: "",
    standard_spu_id: "",
    standard_spu_code: "",
    byname: "",
    spu_name: "",
    unit: "",
    order_num: "",
    order_price: "",
    purchase_price: "",
    remark: "",
    goods_cycle_id: "",
    show: false,
    discount:""
  },
  form: {
    name_uid: "",
    uid: "",
    nickname: "",
    line_id: "",
    salesman_id: "",
    contact: "",
    mobile: "",
    address: "",
    send_time: "",
    send_time_id: 0,
    order_status: 1,
    remark: "",
    zid: null,
    sub_zid: null,
  },
  formsp: {
    name: [],
    girard: null,
    num: "",
    value: "",
  },
  address: {
    address: {
      name: "",
      mobile: "",
      address: "",
      detail: "",
    },
    info: {},
    subAccount: [],
  },
};

import { mapGetters } from "vuex";
import {
  order_base,
  order_getMemberInfo,
  order_copyList,
  order_edit,
  goods_getOfferPrice,
  order_info,
  member_getMembers,
  orderList_delete,
} from "@/api/order/orderList";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import rules from "@/rules/order/orderList_add";
import {
  puzzleImg,
  field_save,
  setting_company,
  goods_getGoodsSpu,
} from "@/api/currency";
import { parseTime } from "@/utils/index";

export default {
  computed: {
    ...mapGetters(["avatar"]),
    total() {
      let sz = 0;
      this.monitorForm.list.forEach((e) => {
        sz += parseFloat(
          this.$toFixed(parseFloat(e.order_num * e.order_price))
        );
      });
      return this.$toFixed(sz);
    },
  },

  components: {},
  data() {
    return {
      // 时间
      parseTime,
      // 验证
      rules,
      // 列表数据
      monitorForm: {
        form: {
          name_uid: "",
          uid: "",
          nickname: "",
          line_id: "",
          salesman_id: "",
          contact: "",
          mobile: "",
          address: "",
          send_time: "",
          send_time_id: 0,
          order_status: 1,
          remark: "",
          zid: null,
          sub_zid: null,
        },
        list: [],
      },
      // 删除商品id
      deleteIds: [],
      activity_type_list: [],
      order_price: "",
      goods_cycle_id: "",
      goods_pre_purchase_price: "",
      goods_pre_price: "",
      address: {
        address: {
          name: "",
          mobile: "",
          address: "",
          detail: "",
        },
        info: {},
        subAccount: [],
        group_id: "",
      },
      order_status: 1,
      puzzleImg,
      info: [],
      // 商品搜索
      formsp: {
        name: [],
        girard: null,
        num: "",
        value: "",
      },
      show: true,
      // 页面设置
      mainor_ex: false,
      display_sub_account: false,
      type: [],
      Header: [
        {
          id: 1,
          name: "序号",
          disabled: true,
        },
        {
          id: 2,
          name: "商品图片",
          disabled: false,
        },
        {
          id: 3,
          name: "商品名称",
          disabled: true,
        },
        {
          id: 4,
          name: "描述",
          disabled: false,
        },
        {
          id: 5,
          name: "单位",
          disabled: true,
        },
        {
          id: 6,
          name: "现有库存",
          disabled: false,
        },
        {
          id: 7,
          name: "起订量",
          disabled: false,
        },
        {
          id: 8,
          name: "指定库存",
          disabled: false,
        },
        {
          id: 9,
          name: "数量",
          disabled: true,
        },

        {
          id: 10,
          name: "采购价",
          disabled: false,
        },
        {
          id: 11,
          name: "单价",
          disabled: true,
        },
        {
          id: 12,
          name: "小计",
          disabled: true,
        },
        {
          id: 13,
          name: "备注",
          disabled: true,
        },
      ],
      // id
      id: this.$route.params.id,
      // 监听滚动
      searchBarFixed: false,
      table_loading: false,
      sort_order_down: true, //true 向下  false  向上
    };
  },
  mounted() {
    window.addEventListener("scroll", this.handleScroll);
  },
  destroyed() {
    window.removeEventListener("scroll", this.handleScroll);
  },
  created() {
    // 信息
    this.order_base();
    this.order_info(this.id);
  },
  methods: {
    // 排序操作
    handleSort() {
      this.sort_order_down = !this.sort_order_down;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        {
          order_add_goods_sort: this.sort_order_down ? 0 : 1,
        }
      );
      setting_company(data).then((res) => {
        if (res.errcode == 0) {
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    pre_price_fliter: function (val) {
      if (val === undefined || val === null || val === "") {
        return false;
      } else {
        return true;
      }
    },
    // 监听滚动
    handleScroll() {
      let scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      scrollTop + document.documentElement.clientHeight >
      document.getElementById("newConBox").offsetTop + 300
        ? (this.searchBarFixed = false)
        : (this.searchBarFixed = true);
    },
    // 用户搜索
    basicquerySearchAsync(queryString, cb) {
      if (queryString) {
        let data = Object.assign(
          {
            sign: Md5(
              objKeySort(
                Object.assign(apiUrl().name, { name: queryString.trim() })
              ) + apiUrl().appsecret
            ),
          },
          apiUrl().name,
          { name: queryString.trim() }
        );
        member_getMembers(data).then((res) => {
          if (res.errcode == 0) {
            var results = res.data.list;
            this.$refs.searchSelect.activated = true;
            cb(results);
          } else {
            this.$message.error(res.errmsg);
            cb([]);
          }
        });
      }
    },
    basicChange(val) {
      this.monitorForm.form.uid = val.id;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, { id: val.id })) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: val.id }
      );
      order_getMemberInfo(data).then((res) => {
        if (res.errcode == 0) {
          this.monitorForm.list = [];
          this.monitorForm.form.send_time_id = parseInt(
            res.data.info.send_time_id
          );
          this.monitorForm.form.nickname = res.data.subAccount[0].nickname;
          this.monitorForm.form.line_id = res.data.info.line_id;
          this.monitorForm.form.salesman_id = res.data.info.salesman_id;
          this.monitorForm.form.contact = res.data.address.name;
          this.monitorForm.form.mobile = res.data.address.mobile;
          this.monitorForm.form.address =
            res.data.address.address + res.data.address.detail;
          this.monitorForm.form.zid = null;
          this.monitorForm.form.sub_zid = null;
          res.data.subAccount.map((e, ind) => {
            if (ind == 0) {
              e.id = 0;
              e.nickname = "主账号";
            }
          });
          this.address = res.data;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 子账号显示
    setting_company() {
      if (!this.display_sub_account) {
        this.order_info(this.id);
      }
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, {
                display_sub_account: this.display_sub_account ? 1 : 0,
              })
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { display_sub_account: this.display_sub_account ? 1 : 0 }
      );
      setting_company(data).then((res) => {
        if (res.errcode == 0) {
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    sub_account(val) {
      this.monitorForm.form.zid = this.address.subAccount[val].id;
      this.address.address = this.address.subAccount[val].address;
      this.monitorForm.form.contact = this.address.subAccount[val].address.name;
      this.monitorForm.form.mobile =
        this.address.subAccount[val].address.mobile;
      this.monitorForm.form.address =
        this.address.subAccount[val].address.address +
        this.address.subAccount[val].address.detail;
    },
    // 商品搜索
    querySearchAsync(queryString, cb) {
      if (this.monitorForm.form.name_uid == "") {
        this.$message.error("用户名称不能为空");
        this.$refs.company.focus();
        cb([]);
        return;
      }
      if (queryString.trim() == "") {
        cb([]);
        return;
      }
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, { name: queryString.trim() })
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { name: queryString.trim() }
      );
      goods_getGoodsSpu(data).then((res) => {
        if (res.errcode == 0) {
          res.data.goods.map((e) => {
            e.name = e.goods.name + "【" + e.spu_unit.title + "】";
          });
          var results = res.data.goods;
          if (this.$refs.searchSelect) {
            this.$refs.searchSelect.activated = true;
          }
          cb(results);
        } else {
          this.$message.error(res.errmsg);
          cb([]);
        }
      });
    },
    // 商品选择
    handleSelect(item) {
      this.formsp.name = item;
      this.formsp.girard = item.id;
      this.goods_getOfferPrice();
      setTimeout(() => {
        this.$refs.focusw2.focus();
      }, 100);
    },
    goods_getOfferPrice() {
      let formsp = JSON.parse(JSON.stringify(this.formsp));
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, {
                uid: this.monitorForm.form.uid,
                group_id: this.address.group_id,
                goods_id: formsp.name.goods_id,
                goods_spu_id: formsp.girard,
              })
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        {
          uid: this.monitorForm.form.uid,
          group_id: this.address.group_id,
          goods_id: formsp.name.goods_id,
          goods_spu_id: formsp.girard,
        }
      );
      goods_getOfferPrice(data).then((res) => {
        if (res.errcode == 0) {
          this.order_price = res.data.price;
          this.goods_cycle_id = res.data.cycle_id;
          this.goods_pre_purchase_price = res.data.pre_purchase_price;
          this.goods_pre_price = res.data.pre_price;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 商品选择确认(回车)
    mainor_sp(e) {
      var keyCode = window.event ? e.keyCode : e.which;
      if (keyCode == 13) {
        this.mainor_qr();
      }
    },
    // 点击添加
    mainor_qr() {
      let formsp = JSON.parse(JSON.stringify(this.formsp));
      if (!formsp.name.name) {
        this.$message.error("请填写商品名称");
        this.$refs.searchSelect.focus();
        return;
      } else if (formsp.num == "") {
        this.$message.error("请填写数量");
        this.$refs.focusw2.focus();
        return;
      } else {
        let monitorForm_list = JSON.parse(
          JSON.stringify(this.monitorForm.list)
        );
        let ind = 0;
        if (monitorForm_list.length != 0) {
          monitorForm_list.map((e, index) => {
            if (e.goods_spu_id == formsp.girard) {
              ind = e.goods_spu_id;
              return;
            }
          });
          if (ind == 0) {
            this.handle(formsp);
          } else {
            setTimeout(() => {
              this.$refs["order_num" + ind][0].focus();
              this.reset();
              ind = 0;
            }, 100);
          }
        } else {
          this.handle(formsp);
        }
      }
    },
    handle(formsp) {
      console.log(formsp)
      let list = JSON.parse(JSON.stringify(shuju.list));
      list.list_index = this.monitorForm.list.length + 1;
      formsp.name.goods.images.map((e) => {
        if (e.main == 1) {
          list.url = this.avatar.domain + e.path;
          list.pic = e.path;
        }
      });
      list.pid = formsp.name.goods.pid;
      list.category_id = formsp.name.goods.category_id;
      list.name = formsp.name.goods.name;
      list.goods_id = formsp.name.goods_id;
      list.standard_goods_id = formsp.name.goods.standard_goods_id;
      list.byname =
        formsp.name.goods.use_byname == 1 ? formsp.name.goods.byname : "";
      list.order_price = this.order_price;
      list.cycle_id = String(this.goods_cycle_id);
      list.pre_purchase_price = this.goods_pre_purchase_price;
      list.pre_price = this.goods_pre_price;
      list.goods_spu_id = formsp.name.id;
      list.standard_spu_id = formsp.name.standard_spu_id;
      list.standard_spu_code = formsp.name.standard_spu_code;
      list.spu_name = formsp.name.spu_name.title;
      list.unit = formsp.name.spu_unit.title;
      list.purchase_price = formsp.name.purchase_price;
      list.order_num = formsp.num;
      list.designated_inventory = formsp.name.designated_inventory;
      list.minimum = formsp.name.minimum;
      list.discount = formsp.name.discount;
      if (formsp.name.inventory) {
        list.num = formsp.name.inventory.num;
      } else {
        list.num = 0;
      }
      list.red = false;
      if (parseFloat(list.purchase_price) > parseFloat(list.order_price)) {
        list.red = true;
      }
      if (this.sort_order_down) {
        this.monitorForm.list.unshift(list);
      } else {
        this.monitorForm.list.push(list);
      }
      this.reset();
      setTimeout(() => {
        this.$refs.searchSelect.focus();
      }, 100);
    },
    reset() {
      this.formsp = JSON.parse(JSON.stringify(shuju.formsp));
      this.show = false;
      this.$nextTick(() => {
        this.show = true;
      });
    },
    // 删除
    scdelete(scope, type) {
      let index = scope.$index;
      if (type) {
        let id = this.monitorForm.list[index].id;
        let activity_type = scope.row.activity_type;
        let activity_type_info = this.activity_type_list[activity_type];
        if (activity_type) {
          let message = `该参加了活动【${activity_type_info}】,确定删除？`;
          this.$confirm(message, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              // this.orderList_delete(id, index);
              this.deleteIds.push(id);
              this.monitorForm.list.splice(index, 1);
            })
            .catch(() => {});
        } else {
          // this.orderList_delete(id, index);
          this.deleteIds.push(id);
          this.monitorForm.list.splice(index, 1);
        }
      } else {
        this.monitorForm.list.splice(index, 1);
      }
    },
    endDelete() {
      this.$message.error("订单最后一个商品无法删除");
    },
    // 删除商品
    orderList_delete(id, index) {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, {
                id: id,
              })
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: id }
      );
      orderList_delete(data).then((res) => {
        if (res.errcode == 0) {
          this.monitorForm.list.splice(index, 1);
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 页面设置
    determine() {
      let arry = [];
      this.Header.map((e) => {
        this.type.map((p) => {
          if (e.id == p) {
            arry.push(e.id);
            return;
          }
        });
      });
      this.field_save(arry);
    },
    recovery() {
      this.field_save([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
    },
    field_save(arry) {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, { type: 4 })) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { type: 4, fields: JSON.stringify(arry) }
        // [1,2,3,4,5,6,7,8,9,10,11]
      );
      field_save(data).then((res) => {
        if (res.errcode == 0) {
          this.$message({
            message: "设置成功",
            type: "success",
          });
          this.order_base();
          this.mainor_ex = false;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 保存
    preservation(type) {
      this.$refs["userListFrom"].validate((valid) => {
        if (valid) {
          if (this.monitorForm.list.length == 0) {
            this.$message.error("请至少添加一个商品");
            return;
          }
          if (type == 3) {
            var { sub_zid, name_uid, ...form } = JSON.parse(
              JSON.stringify(this.monitorForm.form)
            );
            form.zid = form.zid ? form.zid : 0;
          } else {
            var { sub_zid, name_uid, order_status, ...form } = JSON.parse(
              JSON.stringify(this.monitorForm.form)
            );
            form.zid = form.zid ? form.zid : 0;
          }
          let sz = JSON.parse(JSON.stringify(this.monitorForm.list));
          let list = sz.map((e) => {
            let {
              url,
              describe,
              show,
              goods_cycle_id,
              red,
              order_price_copy,
              minimum,
              designated_inventory,
              num,
              list_index,
              pre_price,
              pre_purchase_price,
              activity_type,
              ...sz
            } = e;
            return { ...sz };
          });
          let data = Object.assign(
            {
              sign: Md5(
                objKeySort(Object.assign(apiUrl().name, { id: this.id })) +
                  apiUrl().appsecret
              ),
            },
            apiUrl().name,
            form,
            { delete_ids: this.deleteIds.join(",") },
            { list: JSON.stringify(list), id: this.id }
          );

          order_edit(data).then((res) => {
            if (res.errcode == 0) {
              this.$message({
                message: "保存成功",
                type: "success",
              });
              this.order_info(this.id);
              if (type != 1) {
                this.$router.go(-1);
              }
            } else {
              this.$message.error(res.errmsg);
            }
          });
        }
      });
    },
    // 信息
    order_base() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name
      );
      order_base(data).then((res) => {
        if (res.errcode == 0) {
          this.type = res.data.field;
          this.sort_order_down = !res.data.order_add_goods_sort ? true : false;
          this.display_sub_account =
            res.data.display_sub_account == 1 ? true : false;
          this.info = res.data;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 基本信息
    order_info(id) {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, { id: id })) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: id }
      );
      this.table_loading = true;
      order_info(data).then((res) => {
        if (res.errcode == 0) {
          let address = "";
          let detail = "";
          this.order_status = res.data.info.order_status;
          this.activity_type_list = res.data.activity_type_list;
          this.monitorForm.form.name_uid = res.data.info.member.nickname;
          this.monitorForm.form.uid = res.data.info.member.id;
          this.monitorForm.form.nickname = res.data.info.nickname;
          this.monitorForm.form.line_id = res.data.info.line_id;
          this.monitorForm.form.salesman_id = res.data.info.salesman_id;
          this.monitorForm.form.contact = res.data.info.contact;
          this.monitorForm.form.mobile = res.data.info.mobile;
          this.monitorForm.form.send_time = parseTime(
            res.data.info.send_time,
            "{y}-{m}-{d}"
          );
          this.monitorForm.form.address = res.data.info.address;
          this.monitorForm.form.send_time_id = parseInt(
            res.data.info.send_time_id
          );
          this.monitorForm.form.remark = res.data.info.remark;
          this.monitorForm.form.zid =
            res.data.info.zid == 0 ? 0 : res.data.info.zid;
          this.monitorForm.form.sub_zid =
            res.data.info.zid == 0
              ? 0
              : res.data.subAccount.findIndex((e) => {
                  return e.id == res.data.info.zid;
                });
          if (res.data.info.zid_info) {
            this.address.address.name =
              res.data.subAccount[
                res.data.subAccount.findIndex((e) => {
                  return e.id == res.data.info.zid;
                })
              ].address.name;
            this.address.address.mobile =
              res.data.subAccount[
                res.data.subAccount.findIndex((e) => {
                  return e.id == res.data.info.zid;
                })
              ].address.mobile;
            this.address.address.address =
              res.data.subAccount[
                res.data.subAccount.findIndex((e) => {
                  return e.id == res.data.info.zid;
                })
              ].address.address;
            this.address.address.detail =
              res.data.subAccount[
                res.data.subAccount.findIndex((e) => {
                  return e.id == res.data.info.zid;
                })
              ].address.detail;
          } else {
            this.address.address.name = res.data.subAccount[0].address.name;
            this.address.address.mobile = res.data.subAccount[0].address.mobile;
            this.address.address.address =
              res.data.subAccount[0].address.address;
            this.address.address.detail = res.data.subAccount[0].address.detail;
          }
          res.data.subAccount.map((e, ind) => {
            if (ind == 0) {
              e.id = 0;
              e.nickname = "主账号";
            }
          });
          this.address.subAccount = res.data.subAccount;
          this.address.group_id = res.data.info.member.info.group_id;
          this.standard(res.data.info.order_list);
        } else {
          this.$message.error(res.errmsg);
        }
        this.table_loading = false;
      });
    },
    standard(data) {
      let arry = [];
      data.map((e, index) => {
        console.log(e,'eee')
        arry.push({
          list_index: index + 1,
          id: e.id,
          pid: e.pid,
          activity_type: e.activity_type,
          category_id: e.category_id,
          pic: e.pic,
          url: this.avatar.domain + e.pic,
          name: e.name,
          describe: e.describe,
          goods_id: e.goods_id,
          standard_goods_id: e.standard_goods_id,
          goods_spu_id: e.goods_spu_id,
          standard_spu_id: e.standard_spu_id,
          standard_spu_code: e.standard_spu_code,
          byname: e.byname,
          spu_name: e.spu_name,
          unit: e.unit,
          order_num: e.order_num,
          order_price: e.order_price,
          order_price_copy: e.order_price,
          purchase_price: e.purchase_price,
          pre_purchase_price: e.pre_purchase_price,
          pre_price: e.pre_price,
          remark: e.remark,
          designated_inventory: e.goods_spu.designated_inventory,
          minimum: e.goods_spu.minimum,
          num: e.inventory ? e.inventory.num : "",
          show: true,
          discount:e.discount,
          red:
            parseFloat(e.purchase_price) > parseFloat(e.order_price)
              ? true
              : false,
        });
      });
      this.monitorForm.list = arry;
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.main_name {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 5px 0;
  height: 50px;
  .main_addr {
    color: #999;
  }
  span {
    line-height: 20px;
    display: inline;
    height: 20px;
  }
}
.mainor {
  /deep/.el-button--default {
    border-color: $menuActiveText;
    color: $menuActiveText;
  }
  /deep/.el-form {
    margin-top: 10px;
    /deep/.el-form-item--mini.el-form-item,
    /deep/.el-form-item--small.el-form-item {
      margin-bottom: 10px;
    }
  }
  .remark_in {
    width: 100%;
    /deep/.el-form-item__content {
      width: 80%;
      .el-input {
        width: 66.9%;
      }
    }
  }
  .mainor_zh {
    padding-top: 5px;
    padding-bottom: 5px;
    em {
      color: $colour_18;
      font-size: $font_14;
      display: inline-block;
      width: 90px;
      text-align: right;
      margin-right: 9px;
    }
    span {
      color: $colour_18;
      font-size: $font_14;
      margin-right: 15px;
    }
  }

  .mainor_sp {
    ul {
      margin-right: 20px;
      li {
        margin-bottom: 10px;
        & > span {
          display: inline-block;
          line-height: 32px;
          width: 60px;
          color: $colour_18;
          font-size: $font_14;
          text-align: right;
          em {
            color: $colour_red;
            margin-right: 5px;
          }
        }
        &:first-child {
          & > span {
            width: 85px;
          }
        }
      }
    }
  }
  .newConBox.on {
    width: calc(100% - 230px);
    padding-left: 0;
  }
  .mainor_pr {
    margin-left: 10px;
    margin-bottom: 10px;
    /deep/.el-button--default {
      border-color: $subActive;
      color: $menuText;
      &:hover {
        border-color: $menuActiveText;
        color: $menuActiveText;
      }
    }
    .mainor_prr {
      font-size: $font_16;
      color: $colour_18;
      span {
        color: #fe3b3b;
        margin-left: 10px;
      }
    }
  }
  .mainor_ex {
    position: fixed;
    width: 220px;
    height: 100%;
    background: $white;
    box-shadow: 0 6px 10px rgba(0, 21, 41, 0.08);
    top: 0;
    right: 0;
    z-index: 9999;
    transition: all 0.5s;
    transform: translate3d(100%, 0, 0);
    &.on {
      transform: translate3d(0, 0, 0);
    }
    .mainor_exb {
      position: absolute;
      width: 100%;
      height: calc(100% - 60px);
      overflow-y: auto;
      h3 {
        font-size: $font_16;
        color: $colour_18;
        height: 50px;
        line-height: 50px;
        border-bottom: 1px solid $colour_e6;
        padding-left: 20px;
      }
      .mainor_exb_an {
        padding-left: 20px;
        margin-top: 15px;
        /deep/.el-checkbox {
          display: block;
          margin-top: 15px;
          margin-bottom: 15px;
        }
      }
    }
    .mainor_exb_b {
      position: absolute;
      bottom: 0;
      width: 100%;
      border-top: 1px solid $colour_e6;
      height: 55px;
      text-align: center;
      /deep/.el-button--default {
        margin-top: 12px;
        border-color: $subActive;
        color: $menuText;
        &:hover {
          border-color: $menuActiveText;
          color: $menuActiveText;
        }
      }
    }
  }
  .main_table {
    /deep/.el-table--small td {
      padding: 10px 0;
    }
  }
  .main_taf {
    /deep/.el-table--small td {
      padding-top: 0;
    }
    /deep/.el-form-item--small.el-form-item {
      margin-bottom: 16px;
      margin-top: 16px;
      margin-right: 0;
    }
  }
  .mainor_co {
    position: fixed;
    width: 460px;
    height: 100%;
    background: $white;
    box-shadow: 0 6px 10px rgba(0, 21, 41, 0.08);
    top: 0;
    right: 0;
    z-index: 9999;
    transition: all 0.5s;
    transform: translate3d(100%, 0, 0);
    /deep/.el-button--default {
      border: 0;
    }
    &.on {
      transform: translate3d(0, 0, 0);
    }
    .mainor_cob_c {
      position: absolute;
      top: 15px;
      right: 15px;
      i {
        font-size: 22px;
        font-weight: bold;
        color: $colour_6;
      }
      &:hover {
        cursor: pointer;
      }
    }
    .mainor_cob_s {
      margin-top: 50px;
      margin-left: 10px;
      margin-right: 10px;
      p {
        font-size: $font_12;
        color: $colour_red;
        margin-top: 10px;
      }
      /deep/.el-input-group {
        width: 100%;
      }
    }
    .main_table {
      margin-left: 0;
      position: fixed;
      top: 0;
      height: calc(100% - 120px);
      width: 100%;
      margin-top: 120px;
      /deep/.el-table__body-wrapper {
        border-left: 0;
        table {
          border-left: 1px solid #ebeef5;
        }
      }
      /deep/.el-table__fixed-body-wrapper {
        border-right: 0;
        table {
          border-right: 1px solid #ebeef5;
        }
      }
      .main_table_p {
        margin-left: 10px;
        margin-right: 10px;
        height: 100%;
      }
      /deep/.el-table th .cell {
        font-size: $font_14;
      }
    }
  }
  .main_dialog .main_table {
    /deep/.el-table__body-wrapper {
      overflow-y: auto;
      height: 360px;
    }
  }
}
.order_num_tips {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  color: #999;
  font-size: 14px;
}
.new_navbar_svg {
  transform: rotate(180deg);
  transition: all 500ms ease;
}
.new_navbar_svg2 {
  transition: all 500ms ease;
}

.svg_box {
  display: flex;
  align-items: center;
  margin-left: 15px;
  .navbar_svg {
    margin-left: 8px;
  }
}
.active1 {
  // 满减
  color: #118ab2;
}
.active2 {
  // 满赠
  color: #03a98e;
}
.active3 {
  // 特卖
  color: #aacd6e;
}
.active4 {
  // 积分
  color: #073b4c;
}
.active5 {
  // 曾品
  color: #f56c6c;
}
</style>
